package com.mouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mouse.entity.*;
import com.mouse.entity.vo.GeneCategoryVO;
import com.mouse.entity.vo.GeneCompositionVO;
import com.mouse.mapper.*;
import com.mouse.service.GeneCompositionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mouse.util.StringUtil;
import org.apache.shiro.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author godson
 * @since 2022-08-07
 */
@Service
public class GeneCompositionServiceImpl extends ServiceImpl<GeneCompositionMapper, GeneComposition> implements GeneCompositionService {

    @Autowired
    GeneCategoryMapper geneCategoryMapper;

    @Autowired
    private DiseaseMapper diseaseMapper;

    @Autowired
    private DoubleAllelesMapper doubleAllelesMapper;

    @Autowired
    private PhenotypeMapper phenotypeMapper;

    @Autowired
    private ReferenceMapper referenceMapper;

    @Autowired
    private GeneCompositionDoubleAllelesMapper geneCompositionDoubleAllelesMapper;

    @Autowired
    private MouseMapper mouseMapper;

    @Autowired
    private GeneReferenceMapper geneReferenceMapper;

    @Autowired
    private GeneCompositionReferenceMapper geneCompositionReferenceMapper;

    @Autowired
    private GenecategoryGenecompositionMapper genecategoryGenecompositionMapper;

    @Autowired
    private GeneAlleleMapper geneAlleleMapper;
    @Autowired
    private GeneHomologyMapper geneHomologyMapper;

    @Autowired
    private DoubleAllelesGeneAlleleMapper doubleAllelesGeneAlleleMapper;


    /***
     * 查询：根据一个字段信息查询表格信息，支持字段值类型为ID和String
     * @param page 分页设置
     * @param key 字段名
     * @param value 字段值
     * @return 查询结果
     */
    @Override
    public IPage<GeneComposition> getInfoByMap(IPage<GeneComposition> page, String key, String value) {
        long id;
        QueryWrapper<GeneComposition> wrapper = new QueryWrapper<>();
        if (StringUtil.isInteger(value)) {
            id = Long.parseLong(value);
            wrapper.eq(key, id);
        } else {
            wrapper.eq(key, value);
        }


        return baseMapper.selectPage(page, wrapper);
    }

    @Override
    public GeneCompositionVO getGeneCompositionVO(GeneComposition geneComposition) {



        /**
         * key:gcom.gcid
         */
        Map<Long, List<GeneCategory>> geneCategoryMap = new HashMap<>();

        /**
         * key:gc.gid
         */
        Map<Long, List<Disease>> diseaseMap = new HashMap<>();


        /**
         *  key:gcom.gcid
         */

        Map<Long, List<DoubleAlleles>> doubleAllelesMap = new HashMap<>();

        /**
         * key:da.did
         */
        Map<Long, List<GeneAllele>> geneAlleleMap = new HashMap<>();

        /**
         /**
         * key:gcom.gcid
         */
        Map<Long, List<Phenotype>> phenotypeMap = new HashMap<>();

        /**
         * key:gcom.gcid
         */
        Map<Long, List<Reference>> geneCompositionReferenceMap = new HashMap<>();
        Long id = geneComposition.getGcid();

        //GeneCategory相关
        List<GenecategoryGenecomposition> genecategoryGenecompositionByGcID = genecategoryGenecompositionMapper.getGenecategoryGenecompositionByGcID(id);
        List<GeneCategory> geneCategoryByGcID = geneCategoryMapper.getGeneCategoryByGcID(id);
        for(GenecategoryGenecomposition relation: genecategoryGenecompositionByGcID){
            geneCategoryMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (GeneCategory entity:geneCategoryByGcID){
                if(entity.getGid().equals(relation.getGid())) {
                    geneCategoryMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        //
        List<GeneHomology> geneHomologyByGcid = geneHomologyMapper.getGeneHomologyByGcid(id);
        List<Disease> diseaseByGcid = diseaseMapper.getDiseaseByGcid(id);
        for(GeneHomology relation: geneHomologyByGcid){
            diseaseMap.computeIfAbsent(relation.getGid(), k -> new ArrayList<>());
            for (Disease entity:diseaseByGcid){
                if(entity.getSid().equals(relation.getSid())) {
                    diseaseMap.get(relation.getGid()).add(entity);
                }
            }
        }

        //DoubleAlleles相关
        List<GeneCompositionDoubleAlleles> geneCompositionDoubleAllelesByGcid = geneCompositionDoubleAllelesMapper.getGeneCompositionDoubleAllelesByGcid(id);
        List<DoubleAlleles> doubleAllelesByGcid = doubleAllelesMapper.getDoubleAllelesByGcid(id);
        for(GeneCompositionDoubleAlleles relation: geneCompositionDoubleAllelesByGcid){
            doubleAllelesMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (DoubleAlleles entity:doubleAllelesByGcid){
                if(entity.getDid().equals(relation.getDid())) {
                    doubleAllelesMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        List<DoubleAllelesGeneAllele> doubleAllelesGeneAlleleByGcid = doubleAllelesGeneAlleleMapper.getDoubleAllelesGeneAlleleByGcid(id);
        List<GeneAllele> geneAllelesByGcid = geneAlleleMapper.getGeneAllelesByGcid(id);
        for(DoubleAllelesGeneAllele relation: doubleAllelesGeneAlleleByGcid){
            geneAlleleMap.computeIfAbsent(relation.getDid(), k -> new ArrayList<>());
            for (GeneAllele entity:geneAllelesByGcid){
                if(entity.getAid().equals(relation.getAid())) {
                    geneAlleleMap.get(relation.getDid()).add(entity);
                }
            }
        }
        //Phenotype相关
        List<Mouse> mouseByGcID = mouseMapper.getMouseByGcID(id);
        List<Phenotype> phenotypeByGcID = phenotypeMapper.getPhenotypeByGcID(id);
        for(Mouse relation: mouseByGcID){
            phenotypeMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Phenotype entity:phenotypeByGcID){
                if(entity.getPid().equals(relation.getPid())) {
                    phenotypeMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        //Reference相关
        List<GeneCompositionReference> geneCompositionReferenceByGcID = geneCompositionReferenceMapper.getGeneCompositionReferenceByGcID(id);
        List<Reference> referenceAboutGeneCompositionByGcID = referenceMapper.getReferenceAboutGeneCompositionByGcID(id);
        for(GeneCompositionReference relation: geneCompositionReferenceByGcID){
            geneCompositionReferenceMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Reference entity:referenceAboutGeneCompositionByGcID){
                if(entity.getRid().equals(relation.getRid())) {
                    geneCompositionReferenceMap.get(relation.getGcid()).add(entity);
                }
            }
        }



        GeneCompositionVO geneCompositionVO = new GeneCompositionVO();
        geneCompositionVO.setGeneComposition(geneComposition);
        geneCompositionVO.setGeneCategoryMap(geneCategoryMap);
        geneCompositionVO.setDiseaseMap(diseaseMap);
        geneCompositionVO.setDoubleAllelesMap(doubleAllelesMap);
        geneCompositionVO.setGeneAlleleMap(geneAlleleMap);
        geneCompositionVO.setPhenotypeMap(phenotypeMap);
        geneCompositionVO.setGeneCompositionReferenceMap(geneCompositionReferenceMap);

    return geneCompositionVO;

    }


}
